package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.util.BLuntil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class FixplanHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    /**
     * 检修计划设置节点自定义审批人
     *
     * @param app
     * @param id
     * @param flowNode
     * @param vars
     * @return
     * @throws Exception
     */
    public String addFlowCandidate(String app, Long id, FlowNode flowNode, Map<String, Object> vars) throws Exception {
        String executer = null;
        Map<String, String> map = new HashMap<>();
        map.put("sbcz", " and position like '%厂长%' ");
        map.put("fcsbkz", " and position like '%科长%' ");
//        map.put("hzry", " and position like '%汇总人员%' ");//增加角色   （汇总人员）
//        map.put("fcjxbzz", " and( position like '%班长%' or position like '%组长%'  or position like '%班组长%')");
        if (StringUtils.isNotBlank(map.get(flowNode.getId()))) {
//            log.info("流程进来了");
            //拿到deptcode
            List<Map<String, Object>> defectreport = handlerService.findBySql("select deptcode from bl_fixplan where id=" + id);
            String deptcode = MapUtil.getStr(defectreport.get(0), "deptcode");
            //拿到username
            StringBuffer sbf = new StringBuffer("select u.username from bl_person p left join sys_user u on p.personid = u.worknumber where LEFT(p.deptcode,6)=  LEFT('" +deptcode +"',6)  " + MapUtils.getString(map, flowNode.getId()));
            List<Map<String, Object>> persons = handlerService.findBySql(sbf.toString());
//            log.info("人员查到了" + persons.toString());
            if (CollectionUtils.isNotEmpty(persons)) {
                //人员以逗号隔开
                StringJoiner sj = new StringJoiner(",", "", "");
                for (Map<String, Object> username : persons) {
                    sj.add(MapUtils.getString(username, "username"));
                }
                executer = sj.toString();

            }
        }
        return executer;
    }

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("resp_code", 1);
        result.put("resp_msg", "操作失败");
//        if ("getFixPlanLine".equals(type)) {//获取检修计划行信息
//            String fixnum = MapUtils.getString(params, "plannum"); //获取参数 ， 检修计划编号
////            List<Map<String, Object>> fixlLineList = handlerService.findBySql("select * from bl_planitems where plannum=" + fixnum);
//            return BLuntil.toPage(params, handlerService, "select * from bl_planitems where isfinish<>'完成' and plannum=" + fixnum);
//
//        }
        if("workshopFixPlan".equals(type)){//检修计划按钮接口(汇总检修计划)
            //获取参数  检修计划的年分，月份
            String year = MapUtils.getString(params, "year");//年份 检修计划的数据
            String month = MapUtils.getString(params, "month");//月份   检修计划的数据
            String deptcode=MapUtils.getString(params, "deptcode");// 检修计划的数据
            String plannum=MapUtils.getString(params, "plannum");// 检修计划的数据

            if(deptcode != null && !"".equalsIgnoreCase(deptcode)&&plannum != null && !"".equalsIgnoreCase(plannum)){
                //将汇总过的车间检修计划状态置为已汇总
                handlerService.updateBySql(" update bl_fixplan set status='已汇总'  " +
                        "   WHERE fixtype='车间检修计划' and year='"+year+"' and month='"+month+"'  " +
                        "   and left(deptcode,6)='"+deptcode+"'  ");
                handlerService.updateBySql(
                        " UPDATE bl_planitems  SET FLAG=1,PLANNO='"+plannum+"' WHERE  ID IN (  " +
                                " select *  from ( "+
                                " SELECT PLAN.ID from bl_planitems plan    " +
                                " LEFT JOIN bl_fixplan fix on  fix.plannum=plan.plannum  " +
                                " WHERE fix.fixtype='车间检修计划' and year='"+year+"' and month='"+month+"'   " +
                                "  AND left(fix.deptcode,6)='"+deptcode+"'  " +
                                " AND plan.flag is null ) a )");
                result.put("resp_code", 0);
                result.put("resp_msg", "操作成功");
                return result;
            }
            result.put("resp_code", 1);
            result.put("resp_msg", "请先保存数据");
            return result;

        }
        /**
         * 检修计划图形化界面接口（柱状图）
         *
         */

        if ("fixplanGraph".equalsIgnoreCase(type)) {
            //获取用户编号
            String userName = MapUtils.getString(params, "username");
            //判断前台传入的用户名参数是否为空
            if (userName != null && !"".equalsIgnoreCase(userName)) {
                //获取用户部门信息
                List<Map<String, Object>> list = handlerService.findBySql(
                        " select user.username,user.deptid,dept.depttype,dept.pcode  from sys_user user  " +
                                " LEFT JOIN sys_dept dept on  user.deptid=dept.code " +
                                " where user.username='"+userName+"' ");
                String deptid = MapUtils.getString(list.get(0), "deptid"); //code
                String depttype = MapUtils.getString(list.get(0), "depttype");
                String deptname = MapUtils.getString(params, "deptname");//超链接使用参数部门名字（需前台提供参数）
                //判断前台提供的部门名字是否为空
                if (deptname != null && !"".equals(deptname)) {
                    //不为空的情况下，根据部门名字去查询code
                    List<Map<String, Object>> list1 = handlerService.findBySql("select code from  sys_dept where fullname='"+deptname+"'");
                    //超链接实际上就是阶级段划分（查看各个分厂的各个班组）
                    depttype = "3";
                    //令code的值等于deptid
                    deptid = MapUtils.getString(list1.get(0), "code");
                }
                //如果dept表中的depttype值不是1，2，3，4，110的话，就直接跳出方法
                if (!"1,2,3,4,110".contains(depttype)) {
                    return result;
                }
                String sql = "";
                if ("1".equalsIgnoreCase(depttype)) {//总公司
                    sql = "  SELECT code,fullname,sum(comp1),sum(comp2),sum(comp3),sum(comp4), FORMAT(ifnull((SUM(comp1)/SUM(comp3)),0)*100,2) JXWCL,FORMAT(ifnull((SUM(comp2)/SUM(comp4)),0)*100,2) DQWCL FROM( " +
                            " SELECT dept.code,dept.fullname,count(line.id) comp1,0 AS comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "    UNION  " +
                            "     SELECT dept.code,dept.fullname,0 AS comp1,count(line.id) comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +

                            "     UNION  " +
                            "    SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2,count(line.id) comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "    and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "      " +
                            "    UNION  " +

                            "     SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2 ,0 AS comp3 ,count(line.id) comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "    and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "      " +
                            "      " +
                            "    )  a group by code,fullname";
                } else if ("2".equalsIgnoreCase(depttype) || "110".equalsIgnoreCase(depttype)) {//代表装备能源部，包铝集团，华云新材料等公司，110代表装备能源部
                    sql = "  SELECT code,fullname,sum(comp1),sum(comp2),sum(comp3),sum(comp4), FORMAT(ifnull((SUM(comp1)/SUM(comp3)),0)*100,2) JXWCL,FORMAT(ifnull((SUM(comp2)/SUM(comp4)),0)*100,2) DQWCL FROM(  " +
                            " SELECT dept.code,dept.fullname,count(line.id) comp1,0 AS comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "    UNION  " +
                            "     SELECT dept.code,dept.fullname,0 AS comp1,count(line.id) comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +

                            "     UNION  " +
                            "    SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2,count(line.id) comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "    and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "      " +
                            "    UNION  " +

                            "     SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2 ,0 AS comp3 ,count(line.id) comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.planno   " +
                            "    and line.fixtype='定期维护'" +
                            "      WHERE dept.depttype=3   " +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "      " +
                            "      " +
                            "    )  a group by code,fullname";
                } else if ("3".equalsIgnoreCase(depttype)) {//各个分厂（比如电解二厂等）
                    sql = "  SELECT code,fullname,sum(comp1),sum(comp2),sum(comp3),sum(comp4), FORMAT(ifnull((SUM(comp1)/SUM(comp3)),0)*100,2) JXWCL,FORMAT(ifnull((SUM(comp2)/SUM(comp4)),0)*100,2) DQWCL FROM(  " +
                            " SELECT dept.code,dept.fullname,count(line.id) comp1,0 AS comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.pcode='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "    UNION  " +
                            "     SELECT dept.code,dept.fullname,0 AS comp1,count(line.id) comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.pcode='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +

                            "     UNION  " +
                            "    SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2,count(line.id) comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "    and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.pcode='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "      " +
                            "    UNION  " +

                            "     SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2 ,0 AS comp3 ,count(line.id) comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "    and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.pcode='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +

                            "    )  a group by code,fullname";
                } else if ("4".equalsIgnoreCase(depttype)) {//车间
                    sql = "  SELECT code,fullname,sum(comp1),sum(comp2),sum(comp3),sum(comp4), FORMAT(ifnull((SUM(comp1)/SUM(comp3)),0)*100,2) JXWCL,FORMAT(ifnull((SUM(comp2)/SUM(comp4)),0)*100,2) DQWCL FROM(  " +
                            " SELECT dept.code,dept.fullname,count(line.id) comp1,0 AS comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.code='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +

                            "    UNION  " +
                            "     SELECT dept.code,dept.fullname,0 AS comp1,count(line.id) comp2,0 AS comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "     and line.isfinish=  '完成'  and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.code='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +

                            "     UNION  " +
                            "    SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2,count(line.id) comp3,0 AS comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "    and line.fixtype=  '计划检修'   " +
                            "      WHERE dept.code='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "      " +
                            "    UNION  " +

                            "     SELECT dept.code,dept.fullname,0 AS comp1,0 AS comp2 ,0 AS comp3 ,count(line.id) comp4 FROM  sys_dept  dept  " +
                            "     LEFT JOIN bl_fixplan fix ON  dept.code=fix.deptcode  and fix.YEAR=YEAR(NOW()) and fix.month=month(NOW())   " +
                            "    LEFT JOIN bl_planitems line ON fix.plannum=line.plannum   " +
                            "    and line.fixtype=  '定期维护'   " +
                            "      WHERE dept.code='"+deptid+"'" +
                            "    GROUP BY dept.code,dept.fullname   " +
                            "    )  a group by code,fullname";
                }
                //pc端用以下格式进行返回(一个map里面放3个list)
                List<Map<String, Object>> powerList = handlerService.findBySql(sql);
                Map<String, Object> resultMap = new HashMap<String, Object>();
                List<Object> resuleList1 = new ArrayList<Object>();
                List<Object> resuleList2 = new ArrayList<Object>();
                List<Object> resuleList3 = new ArrayList<Object>();
                for (int i = 0; i < powerList.size(); i++) {
                    resuleList1.add(powerList.get(i).get("fullname"));//x轴  各个分厂的名字
                    resuleList2.add(powerList.get(i).get("JXWCL"));//y轴检修完成率
                    resuleList3.add(powerList.get(i).get("DQWCL"));//y轴定期完成率
                }
                resultMap.put("fullname", resuleList1);
                resultMap.put("plannedmaintenance", resuleList2);
                resultMap.put("regularmaintenance", resuleList3);
                return resultMap;
//                移动端可能会用到以下接口（返回个是为list<map<string,ojbect>>的形式）
//                List<Map<String, Object>> powerList = handlerService.findBySql(sql);
//                List<Object> resuleList = new ArrayList<Object>();
//                for(int i=0;i<powerList.size();i++){
//                    HashMap<String,Object> resultMap=new HashMap<String, Object>() ;
//                    resultMap.put("name",MapUtils.getString(powerList.get(i),"fullname"));
//                    resultMap.put("data",MapUtils.getDouble(powerList.get(i),"WCL"));
//                    resultMap.put("type",MapUtils.getString(powerList.get(i),"fixtype"));
//                    resuleList.add(resultMap);
//                }
//                result.put("series",resuleList);
//                result.put("resp_code",0);
//                result.put("resp_msg","操作成功");
//                return result;
            }
        }
        if ("fixPeople".equalsIgnoreCase(type)) {//检修计划维修负责人接口
            //拿到deptcode
            String deptcode= MapUtils.getString(params,"deptcode");
            String username= BLuntil.getParameter(params,"username");
            String nickname= BLuntil.getParameter(params,"nickname");

            String sql="select * from sys_user where LEFT(deptcode,6)=LEFT('"+deptcode+"',6)  and username  like '%"+username+"%'  and nickname  like '%"+nickname+"%'";
            return BLuntil.toPage(params,handlerService,sql);
        }
        return super.operate(app, type, params);
    }

    @Override
    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {

        // 新增检修计划时 自动生成检修计划描述
        if (!isUpdate) { // 判断是否新增，新增时赋予描述信息

            String desc = MapUtils.getString(data, "deptEnity_fullname") + MapUtils.getString(data, "year") + "年" + MapUtils.getString(data, "month") + "月检修计划";
            data.put("plandesc", desc);
        }


    }
    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {

        //检修计划审核通过后 同步将车间检修计划行中的状态 修改为 ‘已核准’
        if(flowNode.getId().equals("sbcz")){//获取到检修计划的 审核通过的节点名称（最后一个节点  设备厂长）
            boolean flag = op.isFlag();//判断是否审核通过
            //审核通过的情况
            if(flag) {
                Map<String, Object> map = handlerService.findMainById(app, id);//获取检修计划主对象
                String plannum= MapUtils.getString(map,"plannum");//获取检修计划的编号
                String plandesc= MapUtils.getString(map,"plandesc");//获取检修计划的描述
                String deptcode= MapUtils.getString(map,"deptcode");//获取检修计划的所属单位
                String area= MapUtils.getString(map,"area");//获取检修计划的所属区域
                //获取到车间检修计划的编号
                List<Map<String, Object>> planTaskList = handlerService.findBySql(" SELECT DISTINCT  plannum from bl_planitems WHERE planno='"+plannum+"'");
                if(planTaskList!=null && planTaskList.size()>0){
                    for (int i=0;i<planTaskList.size();i++){
                        String planno=MapUtils.getString(planTaskList.get(i),"plannum");
//                        planTaskList.get(i).put("status","已核准");
                        //更改完车间检修计划的状态以后，更新一下检修计划表（车间检修计划表）
//                        handlerService.update("bl_fixplan",planTaskList.get(i));
                        handlerService.updateBySql("UPDATE bl_fixplan set status='已核准' where plannum='"+planno+"'");
                    }

                }
                //获取到车间检修计划中的所有信息
                List<Map<String, Object>> planTaskList1 = handlerService.findBySql(" select a.*, user.nickname,user.id userid from bl_planitems a " +
                        "left join sys_user user on  a.fixpeople=user.username " +
                        "where a.planno='"+plannum+"'");
                if(planTaskList1!=null && planTaskList1.size()>0){
                    for (int i=0;i<planTaskList1.size();i++){
                        String WONUM = handlerService.findBizNum("workorderApp", "bl_workorder");//生成工单编号
                        String fixpeople=MapUtils.getString(planTaskList1.get(i),"fixpeople");//获取到检修计划行信息中的检修负责人
                        String userid=MapUtils.getString(planTaskList1.get(i),"userid");//获取到检修计划行信息中的检修负责人
                        String nickname=MapUtils.getString(planTaskList1.get(i),"nickname");//获取到检修计划行信息中的检修负责人
                        String id1=MapUtils.getString(planTaskList1.get(i),"id");//获取到检修计划行信息的id
                        handlerService.findBySql(" update  bl_planitems set  wonum='"+WONUM+"' where id='"+id1+"'");
                        //生成工单
                        handlerService.findBySql("INSERT  INTO   bl_workorder(WONUM,fixplannum,fixplandesc,fixtext,fixcontent,ASSETNUM,maintenanceprincipal,acceptanceperson,status,deptcode,createuser,createtime,type,area,UDWOTYPE,DESCRIPTION,approveornot) " +
                                " (SELECT  '"+WONUM+"' as WONUM,ifnull(plannum,''),'"+plandesc+"' as fixplandesc,ifnull(equipmentnum,''),ifnull(fixtext,''),ifnull(equipmentnum,''),ifnull(fixpeople,''),ifnull(checkpeople,''),'草稿' as status," +
                                " '"+deptcode+"' AS deptcode,'" +userid+"' as createuser,now() as createtime,'基本工单' as type, '"+area+"' as area,'计划检修工单' as UDWOTYPE,ifnull(fixtext,''),'否'  from bl_planitems where id='"+id1+"');");
                        //启动工单的流程
                        List<Map<String, Object>> reportlist = handlerService.findBySql
                                ("select id from bl_workorder where WONUM='"+WONUM+"'");//获取工单的id
                        Long reportID = MapUtils.getLong(reportlist.get(0), "id");
                        FlowOpinion flowOpinion = new FlowOpinion();
                        flowOpinion.setOpId(fixpeople);
                        flowOpinion.setOpName(nickname);
                        try {
                            handlerService.forcibleStart("workorderApp", reportID, flowOpinion);
                        } catch (Exception e) {
                        }



                    }
                }
            }

        }


        super.afterExcuteFlow(app, id, flowNode, op, event);
    }
    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("bl_planitems".equals(objName)) {//子表
            String assetnums = MapUtils.getString(data, "equipmentnum");//获取设备编号
            String id = MapUtils.getString(data, "id");
            if (assetnums.contains(",")) {
                String[] assetnum = assetnums.split(",");
                for (int i = 0; i < assetnum.length; i++) {
                    String num = assetnum[i];
                    String sql = " INSERT INTO bl_planitems(equipmentnum, assetnums,isfinish, fixunit, plannum, updatetime, createuser, fixtype, finishdate, " +
                            " item, plandate, createtime, reason, fixpeople, fixtext, actualhours, deptcode, version, updateuser, status, " +
                            " checkpeople, planhours,   equipmentname, createuserdesc, actualdate, wonum, planno, flag)  " +
                            " (select '" + num + "'as equipmentnum , assetnums,isfinish, fixunit, plannum, updatetime, createuser, fixtype, finishdate,  " +
                            " item, plandate, createtime, reason, fixpeople, fixtext, actualhours, deptcode, version, updateuser,  " +
                            " status, checkpeople, planhours, (select  equipmentdescribe from bl_ASSET WHERE ASSETNUM='" + num + "') as equipmentname, " +
                            "  createuserdesc, actualdate, wonum, planno, flag  " +
                            " from bl_planitems where id='" + id + "');";
                    handlerService.updateBySql(sql);
                }
                handlerService.updateBySql("delete from bl_planitems where id='" + id + "'");
            }
        }

        super.afterSave(app, objName, data, isUpdate);
    }
       public void timeTStartFlow(Long id, String app) {
           FlowOpinion flowOpinion = new FlowOpinion();
           flowOpinion.setOpId("admin");
           flowOpinion.setOpName("超级管理员");
           try {
            handlerService.forcibleStart(app, id, flowOpinion);
          } catch (Exception e) {
        }
    }

}
